package com.heyqing.topic.CanConstruct_0383;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * ClassName:CanConstruct
 * Package:com.heyqing.topic.CanConstruct_0383
 * Description:
 *
 * @Date:2024/9/27
 * @Author:Heyqing
 */
public class CanConstruct {
    public boolean canConstruct(String ransomNote, String magazine) {
        if (ransomNote.length() > magazine.length()) return false;
        Map<Character, Integer> rMap = new HashMap<>();
        Map<Character, Integer> mMap = new HashMap<>();
        for (int i = 0; i < magazine.length(); i++) {
            mMap.put(magazine.charAt(i), Objects.isNull(mMap.get(magazine.charAt(i))) ? 1 : mMap.get(magazine.charAt(i)) + 1);
            if (i >= ransomNote.length()) continue;
            rMap.put(ransomNote.charAt(i), Objects.isNull(rMap.get(ransomNote.charAt(i))) ? 1 : rMap.get(ransomNote.charAt(i)) + 1);
        }
        for (Character ac : rMap.keySet()) {
            if (Objects.isNull(mMap.get(ac))) return false;
            if (mMap.get(ac) - rMap.get(ac) < 0) return false;
        }
        return true;
    }
}
